/*
 PMProfileManagementViewController.m
 Date Created: February 22, 2010
 
 Author: Frank Lau <flaua@sfu.ca>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "PMProfileManagementViewController.h"
#import "PMCreateProfileViewController.h"


@implementation PMProfileManagementViewController;
@synthesize profileList;
@synthesize activeProfileLabel;
@synthesize profileManagementTableViewController;

- (PMProfileManagementViewController *)initWithProfileList:(PMProfileList *)myprofileList {
  self = [super init];
  if( self ) {
    [self setProfileList:myprofileList];
  }
  return self;
}


- (void)viewDidLoad {
  [super viewDidLoad];
  [self setTitle:@"Profile Management"];
	
  self.profileManagementTableViewController.profileList=self.profileList;
}


- (IBAction)showCreateProfile {
	PMCreateProfileViewController *createProfileViewController=(PMCreateProfileViewController *)[[PMCreateProfileViewController alloc] initWithProfileList:[self profileList]];
	
	[[self navigationController] pushViewController:createProfileViewController animated:YES];
	[createProfileViewController release];	
}

- (void)viewWillAppear:(BOOL)animated {
  
	[super viewWillAppear:animated];
  [[self navigationController] setNavigationBarHidden:NO animated:animated];
		
	// show active profile
	PMProfile *activeProfile=[profileList activeProfile];
	[activeProfileLabel setText:activeProfile.profileName];
	[profileManagementTableViewController.tableView reloadData]; //show the profile list in the table
}

// Called when something was selected in the table view containing the list of profiles
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
  PMProfile *profile=[[profileList listAllProfiles] objectAtIndex:indexPath.row];
  [profileList setActiveProfile:profile];
  
	[activeProfileLabel setText:profile.profileName];  
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  PMProfile *profile=[[profileList listAllProfiles] objectAtIndex:indexPath.row];
  if(profile.active) return UITableViewCellEditingStyleNone;
  return UITableViewCellEditingStyleDelete;
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
